package com.kaili.db.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.kaili.client.service.ISubEwbGenerator;
import com.kaili.common.OperationType;
import com.kaili.common.stock.StockInfoBean;
import com.kaili.common.stock.StockPdaInfoBean;
import com.kaili.pojo.entity.HsEwbSubStock;
import com.kaili.db.mapper.HsEwbSubStockMapper;
import com.kaili.db.service.IHsEwbSubStockService;
import com.kaili.common.exception.BaseExcept;
import com.kaili.utils.Annotation.CheckFieldTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

import static com.kaili.except.ResponseStatus.*;

/**
 * <p>
 * 子单库存 服务实现类
 * </p>
 *
 * @author cgm
 * @since 2018-06-05
 */
@Service
public class HsEwbSubStockServiceImpl extends ServiceImpl<HsEwbSubStockMapper, HsEwbSubStock> implements IHsEwbSubStockService {
    @Autowired
    ISubEwbGenerator subEwbGenerator;

    /**
     * 根据网点ID ,托运单号,库存状态获取主单所属子单库存数
     *
     * @param siteId      网点ID
     * @param ewb         托运单号
     * @param stockStatus 库存状态
     * @return
     */
    @Override
    public int getStockBySiteId(int siteId, String ewb, int stockStatus) {
        Wrapper<HsEwbSubStock> where = new EntityWrapper<>();
        where.where("ewb_no={0} and site_id={1} and status={2}", ewb, siteId, stockStatus);
        return this.selectCount(where);
    }

    @Override
    public List<HsEwbSubStock> getStockListBySiteId(int siteId, String ewb, int stockStatus) {
        Wrapper<HsEwbSubStock> where = new EntityWrapper<>();
        where.where("ewb_no={0} and site_id={1} and status={2}", ewb, siteId, stockStatus);
        return this.selectList(where);

    }

    /**
     * 新增运单调用库存添加信息
     *
     * @param bean
     * @return
     */
    @Transactional
    @Override
    /**
     * 录单添加子单库存
     */
    public int ewbAddStock(StockInfoBean bean) {
        try {
            if (!CheckFieldTools.checkField(bean)) {
                throw new BaseExcept(DATA_STRUCT_ERR);
            }
            List<HsEwbSubStock> stockList = new ArrayList<>();
            List<String> subEwbs = subEwbGenerator.getSubEwb(bean.getEwbNo(), bean.getSize());
            for (int i = 0; i < bean.getSize(); i++) {
                HsEwbSubStock stock = new HsEwbSubStock();
                stock.setSiteId(bean.getSiteId());
                stock.setEwbNo(bean.getEwbNo());
                stock.setSubEwb(subEwbs.get(i));
                stock.setStatus(OperationType.STOCK_ON);
                stock.setCreatedTime(new Date());
                stockList.add(stock);
            }
            baseMapper.insertBatch(stockList);

        } catch (Exception e) {
            e.printStackTrace();
            if (e instanceof BaseExcept) {
                throw new BaseExcept(((BaseExcept) e).getResponseStatus());
            } else {
                throw new BaseExcept(DATA_DUPLICATE);
            }
        }
        return 0;
    }

    /**
     * 录单修改库存
     *
     * @param bean
     * @return
     */
    @Transactional
    @Override
    public int ewbModifyStock(StockInfoBean bean) {
        try {
            if (!CheckFieldTools.checkField(bean)) {
                throw new BaseExcept(DATA_STRUCT_ERR);
            }
            Map search = new HashMap();
            search.put("ewb_no", bean.getEwbNo());
            search.put("site_id", bean.getSiteId());
            this.deleteByMap(search);
            return ewbAddStock(bean);
        } catch (Exception e) {
            e.printStackTrace();
            if (e instanceof BaseExcept) {
                throw new BaseExcept(((BaseExcept) e).getResponseStatus());
            } else {
                throw new BaseExcept(SYSTEM_EXCPTION_ERR);
            }
        }
    }

    /**
     * 删除运单,则删除所有库存信息
     *
     * @param ewbNo
     * @return
     */
    @Transactional
    @Override
    public int deleteEwb(String ewbNo) {
        if (ewbNo != null && ewbNo.length() > 0) {
            Map search = new HashMap();
            search.put("ewb_no", ewbNo);
            this.deleteByMap(search);
        }
        return 0;
    }

    /**
     * BS端新增发件记录
     *
     * @param bean
     * @return
     */
    @Transactional
    @Override
    public int sendEwbByBs(StockInfoBean bean) {
        try {
            if (!CheckFieldTools.checkField(bean) || bean.getNextSiteId() == null) {
                throw new BaseExcept(DATA_STRUCT_ERR);
            }
            Wrapper<HsEwbSubStock> where = new EntityWrapper<>();
            where.where("ewb_no={0} and site_id={1} and status={2}", bean.getEwbNo(), bean.getSiteId(), OperationType.STOCK_STOWAGE);
            List<HsEwbSubStock> stocks = this.selectList(where);
            if (stocks.size() < bean.getSize()) {
                throw new BaseExcept(NOT_ENOUGH_STOCK);
            }
            Map updateInfo = new HashMap();
            updateInfo.put("siteId", bean.getSiteId());
            updateInfo.put("status", OperationType.STOCK_SEND_ON_THE_WAY);
            List<HsEwbSubStock> subs = null;
            if (stocks.size() == bean.getSize()) {
                subs = stocks;
            } else {
                subs = new ArrayList<>();
                for (int i = 0; i < bean.getSize(); i++) {
                    subs.add(stocks.get(i));
                }
            }
            updateInfo.put("subs", subs);
            baseMapper.updateBatch(updateInfo);
            for (int i = 0; i < subs.size(); i++) {
                HsEwbSubStock hsEwbSubStock = subs.get(i);
                hsEwbSubStock.setSiteId(bean.getNextSiteId());
                hsEwbSubStock.setStatus(OperationType.STOCK_ARRIVE_ON_THE_WAY);
            }
            baseMapper.insertBatch(subs);
        } catch (Exception e) {
            e.printStackTrace();
            if (e instanceof BaseExcept) {
                throw new BaseExcept(((BaseExcept) e).getResponseStatus());
            } else {
                throw new BaseExcept(SYSTEM_EXCPTION_ERR);
            }
        }
        return 0;
    }

    /**
     * BS端到件确认-处理库存
     * @param bean 库存对象
     * @param isCommit 是否提交入库,true:提交入库,false:入库回滚
     */
    @Transactional
    @Override
    public void arriveEwbByBs(StockInfoBean bean,boolean isCommit) {
        try {
            if (!CheckFieldTools.checkField(bean) || bean.getPreSiteId() == null) {
                throw new BaseExcept(DATA_STRUCT_ERR);
            }
            int readCurrentSiteStatus = OperationType.STOCK_ARRIVE_ON_THE_WAY;
            int updateCurrentSiteStatus = OperationType.STOCK_ON;
            int updateParentSiteStatis = OperationType.STOCK_NULL;
            if(!isCommit){
                readCurrentSiteStatus = OperationType.STOCK_ON;
                updateCurrentSiteStatus = OperationType.STOCK_ARRIVE_ON_THE_WAY;
                updateParentSiteStatis = OperationType.STOCK_SEND_ON_THE_WAY;
            }

            // 1.判断库存是否充足
            Wrapper<HsEwbSubStock> where = new EntityWrapper<>();
            where.where("ewb_no={0} and site_id={1} and status={2}", bean.getEwbNo(), bean.getSiteId(), readCurrentSiteStatus);
            List<HsEwbSubStock> stocks = this.selectList(where);
            if (stocks.size() < bean.getSize()) {
                throw new BaseExcept(NOT_ENOUGH_STOCK);
            }

            // 2.修改当前站库存
            Map updateInfo = new HashMap();
            updateInfo.put("siteId", bean.getSiteId());
            updateInfo.put("status", updateCurrentSiteStatus);
            List<HsEwbSubStock> subs = null;
            if (stocks.size() == bean.getSize()) {
                subs = stocks;
            } else {
                subs = new ArrayList<>();
                for (int i = 0; i < bean.getSize(); i++) {
                    subs.add(stocks.get(i));
                }
            }
            updateInfo.put("subs", subs);
            baseMapper.updateBatch(updateInfo);

            // 3.修改上一站库存,同城配送的话,不用修改上一站
            if(bean.getSiteId().intValue()!=bean.getPreSiteId().intValue()){
                for (int i = 0; i < subs.size(); i++) {
                    HsEwbSubStock hsEwbSubStock = subs.get(i);
                    hsEwbSubStock.setSiteId(bean.getPreSiteId());
                }
                updateInfo.put("siteId", bean.getPreSiteId());
                updateInfo.put("status", updateParentSiteStatis);
                baseMapper.updateBatch(updateInfo);
            }
        } catch (Exception e) {
            e.printStackTrace();
            if (e instanceof BaseExcept) {
                throw new BaseExcept(((BaseExcept) e).getResponseStatus());
            } else {
                throw new BaseExcept(SYSTEM_EXCPTION_ERR);
            }
        }
    }

    /**
     * BS端派件处理库存
     *
     * @param bean
     * @return
     */
    @Transactional
    @Override
    public int dispatchEwbByBs(StockInfoBean bean) {
        try {
            if (!CheckFieldTools.checkField(bean)) {
                throw new BaseExcept(DATA_STRUCT_ERR);
            }
            Wrapper<HsEwbSubStock> where = new EntityWrapper<>();
            where.where("ewb_no={0} and site_id={1} and status={2}", bean.getEwbNo(), bean.getSiteId(), OperationType.STOCK_ON);
            List<HsEwbSubStock> stocks = this.selectList(where);
            if (stocks.size() < bean.getSize()) {
                throw new BaseExcept(NOT_ENOUGH_STOCK);
            }
            Map updateInfo = new HashMap();
            updateInfo.put("siteId", bean.getSiteId());
            updateInfo.put("status", OperationType.STOCK_DISPATCH_ON_THE_WAY);
            List<HsEwbSubStock> subs = null;
            if (stocks.size() == bean.getSize()) {
                subs = stocks;
            } else {
                subs = new ArrayList<>();
                for (int i = 0; i < bean.getSize(); i++) {
                    subs.add(stocks.get(i));
                }
            }
            updateInfo.put("subs", subs);
            baseMapper.updateBatch(updateInfo);
        } catch (Exception e) {
            e.printStackTrace();
            if (e instanceof BaseExcept) {
                throw new BaseExcept(((BaseExcept) e).getResponseStatus());
            } else {
                throw new BaseExcept(SYSTEM_EXCPTION_ERR);
            }
        }
        return 0;
    }

    /**
     * BS端签收处理库存
     *
     * @param bean
     * @return
     */
    @Transactional
    @Override
    public int signEwbByBs(StockInfoBean bean) {
        try {
            if (!CheckFieldTools.checkField(bean)) {
                throw new BaseExcept(DATA_STRUCT_ERR);
            }
            Wrapper<HsEwbSubStock> where = new EntityWrapper<>();
            //派送
            if (bean.getPickGoodsModeId() == OperationType.PICK_MODE_DISPATCH) {
                where.where("ewb_no={0} and site_id={1} and status={2}", bean.getEwbNo(), bean.getSiteId(), OperationType.STOCK_DISPATCH_ON_THE_WAY);
            } else if (bean.getPickGoodsModeId() == OperationType.PICK_MODE_SELF) { //自提
                where.where("ewb_no={0} and site_id={1} and status={2}", bean.getEwbNo(), bean.getSiteId(), OperationType.STOCK_ON);
            }
            List<HsEwbSubStock> stocks = this.selectList(where);
            if (stocks.size() < bean.getSize()) {
                throw new BaseExcept(NOT_ENOUGH_STOCK);
            }
            Map updateInfo = new HashMap();
            updateInfo.put("siteId", bean.getSiteId());
            updateInfo.put("status", OperationType.STOCK_NULL);
            List<HsEwbSubStock> subs = null;
            if (stocks.size() == bean.getSize()) {
                subs = stocks;
            } else {
                subs = new ArrayList<>();
                for (int i = 0; i < bean.getSize(); i++) {
                    subs.add(stocks.get(i));
                }
            }
            updateInfo.put("subs", subs);
            baseMapper.updateBatch(updateInfo);
        } catch (Exception e) {
            e.printStackTrace();
            if (e instanceof BaseExcept) {
                throw new BaseExcept(((BaseExcept) e).getResponseStatus());
            } else {
                throw new BaseExcept(SYSTEM_EXCPTION_ERR);
            }
        }
        return 0;
    }
    /**
     * BS端配置处理库存
     *
     * @param bean
     * @return
     */
    @Transactional
    @Override
    public List<HsEwbSubStock> stowageEwbByBs(StockInfoBean bean) {
        List<HsEwbSubStock> subs = null;
        try {
            if (!CheckFieldTools.checkField(bean)) {
                throw new BaseExcept(DATA_STRUCT_ERR);
            }
            Wrapper<HsEwbSubStock> where = new EntityWrapper<>();
            where.where("ewb_no={0} and site_id={1} and status={2}", bean.getEwbNo(), bean.getSiteId(), OperationType.STOCK_ON);
            List<HsEwbSubStock> stocks = this.selectList(where);
            if (stocks.size() < bean.getSize()) {
                throw new BaseExcept(NOT_ENOUGH_STOCK);
            }
            Map updateInfo = new HashMap();
            updateInfo.put("siteId", bean.getSiteId());
            updateInfo.put("status", OperationType.STOCK_STOWAGE);
            if (stocks.size() == bean.getSize()) {
                subs = stocks;
            } else {
                subs = new ArrayList<>();
                for (int i = 0; i < bean.getSize(); i++) {
                    subs.add(stocks.get(i));
                }
            }
            updateInfo.put("subs", subs);
            baseMapper.updateBatch(updateInfo);
        } catch (Exception e) {
            e.printStackTrace();
            if (e instanceof BaseExcept) {
                throw new BaseExcept(((BaseExcept) e).getResponseStatus());
            } else {
                throw new BaseExcept(SYSTEM_EXCPTION_ERR);
            }
        }
        return subs;
    }

    @Override
    public int transEwbByBs(StockInfoBean bean) {
        try {
            if (!CheckFieldTools.checkField(bean)) {
                throw new BaseExcept(DATA_STRUCT_ERR);
            }
            Wrapper<HsEwbSubStock> where = new EntityWrapper<>();
            where.where("ewb_no={0} and site_id={1} and status={2}", bean.getEwbNo(), bean.getSiteId(), OperationType.STOCK_ON);
            List<HsEwbSubStock> stocks = this.selectList(where);
            if (stocks.size() < bean.getSize()) {
                throw new BaseExcept(NOT_ENOUGH_STOCK);
            }
            Map updateInfo = new HashMap();
            updateInfo.put("siteId", bean.getSiteId());
            updateInfo.put("status", OperationType.STOCK_TRANS);
            List<HsEwbSubStock> subs = null;
            if (stocks.size() == bean.getSize()) {
                subs = stocks;
            } else {
                subs = new ArrayList<>();
                for (int i = 0; i < bean.getSize(); i++) {
                    subs.add(stocks.get(i));
                }
            }
            updateInfo.put("subs", subs);
            baseMapper.updateBatch(updateInfo);
        } catch (Exception e) {
            e.printStackTrace();
            if (e instanceof BaseExcept) {
                throw new BaseExcept(((BaseExcept) e).getResponseStatus());
            } else {
                throw new BaseExcept(SYSTEM_EXCPTION_ERR);
            }
        }
        return 0;
    }

    /**
     * BS端新增发件记录
     *
     * @param bean
     * @return
     */
    @Transactional
    @Override
    public int sendEwbByPda(StockPdaInfoBean bean) {
        try {
            if (!CheckFieldTools.checkField(bean) || bean.getNextSiteId() == null) {
                throw new BaseExcept(DATA_STRUCT_ERR);
            }
            if (bean.getSize() != bean.getSubEwbs().size()) {
                throw new BaseExcept(STOCK_REQUST_SIZE_ERR);
            }
            Map updateInfo = new HashMap();
            updateInfo.put("siteId", bean.getSiteId());
            updateInfo.put("ewbNo", bean.getEwbNo());
            updateInfo.put("status", OperationType.STOCK_STOWAGE);
            updateInfo.put("subs", bean.getSubEwbs());

            List<HsEwbSubStock> stocks = baseMapper.selectBatch(updateInfo);
            if (stocks.size() < bean.getSize()) {
                throw new BaseExcept(NOT_ENOUGH_STOCK);
            }
            updateInfo.put("siteId", bean.getSiteId());
            updateInfo.put("status", OperationType.STOCK_SEND_ON_THE_WAY);
            List<HsEwbSubStock> subs = null;
            if (stocks.size() == bean.getSize()) {
                subs = stocks;
            } else {
                subs = new ArrayList<>();
                for (int i = 0; i < bean.getSize(); i++) {
                    subs.add(stocks.get(i));
                }
            }
            updateInfo.put("subs", subs);
            baseMapper.updateBatch(updateInfo);
            for (int i = 0; i < subs.size(); i++) {
                HsEwbSubStock hsEwbSubStock = subs.get(i);
                hsEwbSubStock.setSiteId(bean.getNextSiteId());
                hsEwbSubStock.setStatus(OperationType.STOCK_ARRIVE_ON_THE_WAY);
            }
            if(bean.getSiteId().intValue() == bean.getNextSiteId().intValue()){
                //同城配送
                updateInfo.put("status", OperationType.STOCK_ARRIVE_ON_THE_WAY);
                baseMapper.updateBatch(updateInfo);
            }else{
                baseMapper.insertBatch(subs);
            }
        } catch (Exception e) {
            e.printStackTrace();
            if (e instanceof BaseExcept) {
                throw new BaseExcept(((BaseExcept) e).getResponseStatus());
            } else {
                throw new BaseExcept(SYSTEM_EXCPTION_ERR);
            }
        }
        return 0;
    }

    /**
     * BS端到件确认处理库存
     *
     * @param bean
     * @return
     */
    @Transactional
    @Override
    public int arriveEwbByPda(StockPdaInfoBean bean) {
        try {
            if (!CheckFieldTools.checkField(bean) || bean.getPreSiteId() == null) {
                throw new BaseExcept(DATA_STRUCT_ERR);
            }
            if (bean.getSize() != bean.getSubEwbs().size()) {
                throw new BaseExcept(STOCK_REQUST_SIZE_ERR);
            }
            Map updateInfo = new HashMap();
            updateInfo.put("siteId", bean.getSiteId());
            updateInfo.put("ewbNo", bean.getEwbNo());
            updateInfo.put("status", OperationType.STOCK_ARRIVE_ON_THE_WAY);
            updateInfo.put("subs", bean.getSubEwbs());

            List<HsEwbSubStock> stocks = baseMapper.selectBatch(updateInfo);
            if (stocks.size() < bean.getSize()) {
                throw new BaseExcept(NOT_ENOUGH_STOCK);
            }
            updateInfo.put("siteId", bean.getSiteId());
            updateInfo.put("status", OperationType.STOCK_ON);
            List<HsEwbSubStock> subs = null;
            if (stocks.size() == bean.getSize()) {
                subs = stocks;
            } else {
                subs = new ArrayList<>();
                for (int i = 0; i < bean.getSize(); i++) {
                    subs.add(stocks.get(i));
                }
            }
            updateInfo.put("subs", subs);
            baseMapper.updateBatch(updateInfo);
            for (int i = 0; i < subs.size(); i++) {
                HsEwbSubStock hsEwbSubStock = subs.get(i);
                hsEwbSubStock.setSiteId(bean.getPreSiteId());
            }
            updateInfo.put("siteId", bean.getPreSiteId());
            updateInfo.put("status", OperationType.STOCK_NULL);
            baseMapper.updateBatch(updateInfo);
        } catch (Exception e) {
            e.printStackTrace();
            if (e instanceof BaseExcept) {
                throw new BaseExcept(((BaseExcept) e).getResponseStatus());
            } else {
                throw new BaseExcept(SYSTEM_EXCPTION_ERR);
            }
        }
        return 0;
    }

    /**
     * BS端派件处理库存
     *
     * @param bean
     * @return
     */
    @Transactional
    @Override
    public int dispatchEwbByPda(StockPdaInfoBean bean) {
        try {
            if (!CheckFieldTools.checkField(bean)) {
                throw new BaseExcept(DATA_STRUCT_ERR);
            }
            Map updateInfo = new HashMap();
            updateInfo.put("siteId", bean.getSiteId());
            updateInfo.put("ewbNo", bean.getEwbNo());
            updateInfo.put("status", OperationType.STOCK_ON);
            updateInfo.put("subs", bean.getSubEwbs());
            List<HsEwbSubStock> stocks = baseMapper.selectBatch(updateInfo);
            if (stocks.size() < bean.getSize()) {
                throw new BaseExcept(NOT_ENOUGH_STOCK);
            }
            updateInfo.put("siteId", bean.getSiteId());
            updateInfo.put("status", OperationType.STOCK_DISPATCH_ON_THE_WAY);
            List<HsEwbSubStock> subs = null;
            if (stocks.size() == bean.getSize()) {
                subs = stocks;
            } else {
                subs = new ArrayList<>();
                for (int i = 0; i < bean.getSize(); i++) {
                    subs.add(stocks.get(i));
                }
            }
            updateInfo.put("subs", subs);
            baseMapper.updateBatch(updateInfo);
        } catch (Exception e) {
            e.printStackTrace();
            if (e instanceof BaseExcept) {
                throw new BaseExcept(((BaseExcept) e).getResponseStatus());
            } else {
                throw new BaseExcept(SYSTEM_EXCPTION_ERR);
            }
        }
        return 0;
    }

    /**
     * BS端签收处理库存
     *
     * @param bean
     * @return
     */
    @Transactional
    @Override
    public int signEwbByPda(StockPdaInfoBean bean) {
        try {
            if (!CheckFieldTools.checkField(bean)) {
                throw new BaseExcept(DATA_STRUCT_ERR);
            }

            Map updateInfo = new HashMap();
            updateInfo.put("siteId", bean.getSiteId());
            updateInfo.put("ewbNo", bean.getEwbNo());
            updateInfo.put("status", OperationType.STOCK_DISPATCH_ON_THE_WAY);
            updateInfo.put("subs", bean.getSubEwbs());
            List<HsEwbSubStock> stocks = baseMapper.selectBatch(updateInfo);
            if (stocks.size() < bean.getSize()) {
                throw new BaseExcept(NOT_ENOUGH_STOCK);
            }
            updateInfo.put("siteId", bean.getSiteId());
            updateInfo.put("status", OperationType.STOCK_NULL);
            List<HsEwbSubStock> subs = null;
            if (stocks.size() == bean.getSize()) {
                subs = stocks;
            } else {
                subs = new ArrayList<>();
                for (int i = 0; i < bean.getSize(); i++) {
                    subs.add(stocks.get(i));
                }
            }
            updateInfo.put("subs", subs);
            baseMapper.updateBatch(updateInfo);
        } catch (Exception e) {
            e.printStackTrace();
            if (e instanceof BaseExcept) {
                throw new BaseExcept(((BaseExcept) e).getResponseStatus());
            } else {
                throw new BaseExcept(SYSTEM_EXCPTION_ERR);
            }
        }
        return 0;
    }

    /**
     * BS端配置处理库存
     *
     * @param bean
     * @return
     */
    @Transactional
    @Override
    public List<HsEwbSubStock> stowageEwbByPda(StockPdaInfoBean bean) {
        List<HsEwbSubStock> subs = null;
        try {
            if (!CheckFieldTools.checkField(bean)) {
                throw new BaseExcept(DATA_STRUCT_ERR);
            }
            Map updateInfo = new HashMap();
            updateInfo.put("siteId", bean.getSiteId());
            updateInfo.put("ewbNo", bean.getEwbNo());
            updateInfo.put("status", OperationType.STOCK_ON);
            updateInfo.put("subs", bean.getSubEwbs());
            List<HsEwbSubStock> stocks = baseMapper.selectBatch(updateInfo);
            if (stocks.size() < bean.getSize()) {
                throw new BaseExcept(NOT_ENOUGH_STOCK);
            }
            updateInfo.put("siteId", bean.getSiteId());
            updateInfo.put("status", OperationType.STOCK_STOWAGE);

            if (stocks.size() == bean.getSize()) {
                subs = stocks;
            } else {
                subs = new ArrayList<>();
                for (int i = 0; i < bean.getSize(); i++) {
                    subs.add(stocks.get(i));
                }
            }
            updateInfo.put("subs", subs);
            baseMapper.updateBatch(updateInfo);


        } catch (Exception e) {
            e.printStackTrace();
            if (e instanceof BaseExcept) {
                throw new BaseExcept(((BaseExcept) e).getResponseStatus());
            } else {
                throw new BaseExcept(SYSTEM_EXCPTION_ERR);
            }
        }
        return subs;
    }

    @Override
    public int transEwbByPda(StockPdaInfoBean bean) {
        try {
            if (!CheckFieldTools.checkField(bean)) {
                throw new BaseExcept(DATA_STRUCT_ERR);
            }
            Map updateInfo = new HashMap();
            updateInfo.put("siteId", bean.getSiteId());
            updateInfo.put("ewbNo", bean.getEwbNo());
            updateInfo.put("status", OperationType.STOCK_ON);
            updateInfo.put("subs", bean.getSubEwbs());
            List<HsEwbSubStock> stocks = baseMapper.selectBatch(updateInfo);
            if (stocks.size() < bean.getSize()) {
                throw new BaseExcept(NOT_ENOUGH_STOCK);
            }
            updateInfo.put("siteId", bean.getSiteId());
            updateInfo.put("status", OperationType.STOCK_TRANS);
            List<HsEwbSubStock> subs = null;
            if (stocks.size() == bean.getSize()) {
                subs = stocks;
            } else {
                subs = new ArrayList<>();
                for (int i = 0; i < bean.getSize(); i++) {
                    subs.add(stocks.get(i));
                }
            }
            updateInfo.put("subs", subs);
            baseMapper.updateBatch(updateInfo);
        } catch (Exception e) {
            e.printStackTrace();
            if (e instanceof BaseExcept) {
                throw new BaseExcept(((BaseExcept) e).getResponseStatus());
            } else {
                throw new BaseExcept(SYSTEM_EXCPTION_ERR);
            }
        }
        return 0;
    }

    @Override
    public int updateBatch(Map updateInfo) {
        baseMapper.updateBatch(updateInfo);
        return 0;
    }

    @Override
    public void pFinDispatchInput(Map info) {
        baseMapper.pFinDispatchInput(info);
    }

    @Override
    public void pFinEwbsTransferInput(Map info) {
        baseMapper.pFinEwbsTransferInput(info);
    }


}
